//
//  JPRedViewController.m
//  Jpod
//
//  Created by zenox on 2022/4/6.
//

#import "JPRedViewController.h"
#import "JPDataReviewModel.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
@interface JPRedViewController ()
{
    AVPlayerViewController      *_playerController;
      AVPlayer                    *_player;
      AVAudioSession              *_session;
      NSString                    *_urlString;
}
@end

@implementation JPRedViewController

//- (void)viewDidLoad {
//    [super viewDidLoad];
//    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor redColor];
//    
//
//    //1、获取URL地址
//       NSURL * url = [[NSBundle mainBundle]URLForResource:@"test.mp4" withExtension:nil];
//       
//       //2、AV播放视图控制器
//       AVPlayerViewController * pVC = [AVPlayerViewController new];
//       
//       //3、创建player
//       pVC.player = [AVPlayer playerWithURL:url];
//       
//       //4、准备播放
//       [pVC.player play];
//       
//       //5、模态视图的弹出
//       //[self presentViewController:pVC animated:YES completion:nil];
//       
//       pVC.view.frame = CGRectMake(0, 0, 200, 200);
//       
//       
//       [self.view addSubview:pVC.view];
//
//
//    
//    
//       //1、创建URL
//       NSURL * url = [[NSBundle mainBundle]URLForResource:@"test.mp4" withExtension:nil];
//       
//       //2、获取资源
//       AVAsset * asset = [AVAsset assetWithURL:url];
//       
//       //3、创建资源图像生成器
//       AVAssetImageGenerator * imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
//       
//       //4、开始生成图像
//       
//       //Times表示影片的时间的值
//       
//       CMTime time = CMTimeMake(60, 1);
//       
//       NSValue * value = [NSValue valueWithCMTime:time];
//       
//       [imageGenerator generateCGImagesAsynchronouslyForTimes:@[value] completionHandler:^(CMTime requestedTime, CGImageRef
//        _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) {
//           
//           //在这里面是子线程来执行的
//           //5、在主线程中更新UI
//           dispatch_sync(dispatch_get_main_queue(), ^{
//               self.imageView.image = [UIImage imageWithCGImage:image];
//           });
//       }];
//
//
//    
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    JPDataReviewModel *dd = [[JPDataReviewModel alloc] init];
    NSInteger i =  dd.materials.lastObject.picUrls.count;
    if ( dd.materials.lastObject.picUrls.count >0) {
        
    }
//    self.view.backgroundColor = [UIColor whiteColor];
//    _session = [AVAudioSession sharedInstance];
//    [_session setCategory:AVAudioSessionCategoryPlayback error:nil];
//
////    [NSURL URLWithString:@"file:///var/mobile/Media/DCIM/100APPLE/IMG_0023.MOV"]
////    [NSURL URLWithString:@"https://vd2.bdstatic.com/mda-nd3zuj2bys8vfusr/sc/cae_h264_delogo/1649117315696043530/mda-nd3zuj2bys8vfusr.mp4"]
//    _player = [AVPlayer playerWithURL:[NSURL URLWithString:@"file:///var/mobile/Media/DCIM/100APPLE/IMG_0023.MOV"]];
//    _playerController = [[AVPlayerViewController alloc] init];
//    _playerController.player = _player;
//    _playerController.videoGravity = AVLayerVideoGravityResizeAspect;
//    _playerController.allowsPictureInPicturePlayback = true;    //画中画，iPad可用
//    _playerController.showsPlaybackControls = true;
//
//    [self addChildViewController:_playerController];
//    _playerController.view.translatesAutoresizingMaskIntoConstraints = true;    //AVPlayerViewController 内部可能是用约束写的，这句可以禁用自动约束，消除报错
//    //self.view.bounds
//    _playerController.view.frame = CGRectMake(0, 0, 320, 300);
//    [self.view addSubview:_playerController.view];
//    [_playerController.player play];    //自动播放
//
//    UIImageView *image = [[UIImageView alloc] init];
//    image.image = [self getVideoFirstViewImage:[NSURL URLWithString:@"file:///var/mobile/Media/DCIM/100APPLE/IMG_0023.MOV"]];
//    image.frame = CGRectMake(0, 400, self.view.frame.size.width, 200);
//    [self.view addSubview:image];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
// 获取视频第一帧
- (UIImage*)getVideoFirstViewImage:(NSURL *)path {
  
  AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:path options:nil];
  AVAssetImageGenerator *assetGen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
  
  assetGen.appliesPreferredTrackTransform = YES;
  CMTime time = CMTimeMakeWithSeconds(0.0, 600);
  NSError *error = nil;
  CMTime actualTime;
  CGImageRef image = [assetGen copyCGImageAtTime:time actualTime:&actualTime error:&error];
  UIImage *videoImage = [[UIImage alloc] initWithCGImage:image];
  CGImageRelease(image);
  return videoImage;
  
}


@end
